<?php

namespace App\Http\Controllers\Api;

use App\Event;
use App\Http\Controllers\Controller;
use App\User;
use Illuminate\Auth\AuthenticationException;
use Illuminate\Http\Request;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\Validator;

class EmployeeController extends Controller
{
    public function index(){
        $this->is_admin();
        $employees = User::all()->makeHidden(['is_admin']);
        return $this->success($employees);
    }
    public function show(User $user){
        $this->is_admin();
        $user->makeHidden(['is_admin']);
        return $this->success($user);
    }
    public function create(Request $request){
//        $this->is_admin();
        $validate = $this->validate($request,[
            'firstname'=>'required',
            'lastname'=>'required',
            'scope'=>'required|in:Course,Training',
            'is_admin'=>'required|boolean',
            'photo'=>'required|image',
            'email'=>'required|email',
            'password'=>'required'
        ]);

        $validator = Validator::make($request->all(),['email'=>'unique:users']);
        if ($validator->fails()) return $this->error_422('This email address already been token');

        $file = $request->file('photo')->store('/public/upload/user/'.Carbon::now()->format('Y-m-d'));
        $photo = Storage::url($file);

        $user = new User($validate);
        $user->photo = $photo;
        $user->password = Hash::make($validate['password']);
        $user->save();

        return $this->success(['id'=>$user->id]);
    }
    public function delete(User $user){
        $this->is_admin();
        $user->delete();
        return $this->success();
    }
    public function accept(Event $event){
        if (empty(auth()->user()->events)) throw new AuthenticationException();
        $event = auth()->user()->events()->find($event->id);
        if (empty($event)) throw new AuthenticationException();

        if ($event->status != 'Pending') return $this->error_422('This event status is not pending');

        $event->pivot->response = 'accept';
        $event->pivot->save();
        $event->accepted();

        return $this->success(['id'=>$event->id,'status'=>$event->status]);
    }
    public function finish(Event $event){
        if (!auth()->user()->is_admin){
            if (empty(auth()->user()->events)) return $this->error_401('the employee is not assigned to the event');
            if (empty(auth()->user()->events()->find($event->id))) return $this->error_401('the employee is not assigned to the event');
            if ($event->status != 'Accepted') return $this->error_422("This event's status is not 'accepted'");
        }

        $event->status = 'Finished';
        $event->save();

        return $this->success(['id'=>$event->id,'status'=>$event->status]);
    }
    public function events(){
        $events = auth()->user()->events->makeHidden(['photo','client_id','pivot'])->each(function ($item){
            $item->response = $item->pivot->response;
        });

        return $this->success($events);
    }
}
